package com.logic.landseaserver.ws;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.endpoint.AbstractEndpoint;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.landseaserver.domain.WeChatUserLogin;
import com.logic.landseaserver.service.IWeChatLoginService;
import com.logic.system.security.UserDetailsService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Path("/wechat")
@Api(value = "WeChat", description = "WeChat Rest WebServices", position = 42)

public class WeChatLoginRest extends AbstractEndpoint {

	private final Logger log = LoggerFactory.getLogger(UserDetailsService.class);

	@Autowired
	private IWeChatLoginService weChatLoginService;

	@POST
	@Path("/loginByCode/{code}")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "wechat login infomation by code")
	public Response queryProjectAppartments(
			@ApiParam(value = "param", required = true) @PathParam("code") String code) {
		CommonResultMap map = new CommonResultMap();
		try {
			logger.info("走进rest了");
			WeChatUserLogin login = weChatLoginService.loginByCode(code);
			map.setData(login);
			map.setResult(CommonResult.SUCCESS);
		} catch (Exception e) {
		    logger.error("queryProjectAppartments error.", e);
			map.setResult(CommonResult.ERROR, e.getMessage());
		}
		return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}

	//获取微信端部门列表
	@GET
	@Path("/getCorporateList")
	@Produces(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "获取微信端部门列表", position = 12)
	public Response getCorporateList() {
		CommonResultMap map = new CommonResultMap();
		String list = null;
		try {
			list = weChatLoginService.getCorporate();
			map.setData(list);
			map.setResult(CommonResult.SUCCESS);
		} catch (Exception e) {
		    logger.error("getCorporateList error.", e);
			map.setResult(CommonResult.ERROR, e.getMessage());
		}
		return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}
}
